package com.punch.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punch.common.exception.BusinessException;
import com.punch.common.utils.ImageUtil;
import com.punch.common.vo.order.TransferOrderVO;
import com.punch.order.exception.OrderExceptionEnum;
import com.punch.order.mapper.TransferOrderMapper;
import com.punch.order.model.CourseSettlement;
import com.punch.order.model.MaterialSettlement;
import com.punch.order.model.TransferOrder;
import com.punch.order.service.CourseSettlementService;
import com.punch.order.service.MaterialSettlementService;
import com.punch.order.service.TransferOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 转账信息 服务实现类
 * </p>
 *
 * @author wjj
 * @since 2020-09-26
 */
@Service
public class TransferOrderServiceImpl extends ServiceImpl<TransferOrderMapper, TransferOrder> implements TransferOrderService {


    @Resource
    private CourseSettlementService courseSettlementService;

    @Resource
    private MaterialSettlementService materialSettlementService;

    /**
     * 获取转账信息
     *
     * @param settlementId
     * @return TransferOrderVO
     */
    @Override
    public TransferOrderVO getTransfer(Long settlementId) {
        QueryWrapper<TransferOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TransferOrder::getSettlementId, settlementId);
        TransferOrder transferOrder = getOne(queryWrapper);
        if (transferOrder == null) {
            throw new BusinessException(OrderExceptionEnum.TRANSFER_NOT_EXIST);
        }
        TransferOrderVO transferOrderVO = new TransferOrderVO();
        BeanUtils.copyProperties(transferOrder, transferOrderVO);
        //处理图片
        if (StrUtil.isNotEmpty(transferOrder.getVoucherUrl())) {
            transferOrderVO.setVoucherUrl(ImageUtil.addPrefix(transferOrder.getVoucherUrl()));
        }
        return transferOrderVO;
    }

    /**
     * 新增结算转账信息
     *
     * @param transferOrderVO 转账信息
     * @return transferId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveTransfer(TransferOrderVO transferOrderVO) {
        TransferOrder transferOrder = new TransferOrder();
        BeanUtils.copyProperties(transferOrderVO, transferOrder);
        //处理图片
        if (StrUtil.isNotEmpty(transferOrderVO.getVoucherUrl())) {
            transferOrder.setVoucherUrl(ImageUtil.removePrefix(transferOrderVO.getVoucherUrl()));
        }
        save(transferOrder);
        //转账以后改成已打款 0-待确认 1-已确认 2-已打款
        if (transferOrderVO.getTransferType() == 0) {
            if (courseSettlementService.getById(transferOrderVO.getSettlementId()) == null) {
                throw new BusinessException(OrderExceptionEnum.COURSE_SETTLEMENT_NOT_EXIST);
            }
            //课程
            CourseSettlement courseSettlement = new CourseSettlement();
            courseSettlement.setSettlementStatus(2);
            courseSettlement.setId(transferOrderVO.getSettlementId());
            courseSettlementService.updateById(courseSettlement);
        }
        if (transferOrderVO.getTransferType() == 1) {
            if (materialSettlementService.getById(transferOrderVO.getSettlementId()) == null) {
                throw new BusinessException(OrderExceptionEnum.MATERIAL_SETTLEMENT_NOT_EXIST);
            }
            //教具
            MaterialSettlement materialSettlement = new MaterialSettlement();
            materialSettlement.setSettlementStatus(2);
            materialSettlement.setId(transferOrderVO.getSettlementId());
            materialSettlementService.updateById(materialSettlement);
        }

        return transferOrder.getId();
    }
}
